<?php
namespace app\erp_meixingtiyu\controller;

use think\facade\Request;
use app\erp_meixingtiyu\model\Arena as ArenaModel;
use app\store\controller\Controller as StoreController;
use app\erp_meixingtiyu\logic\Arena as ArenaLogic;
use app\erp_meixingtiyu\validate\Arena as ArenaValidate;

class Arena extends StoreController
{
    /**
     * 获取所有场地及其半场信息
     */
    public function allArenas()
    {
        $list = ArenaModel::getAllArenasWithParts();
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 获取所有场地分页列表
     */
    public function all()
    {
        $list = (new ArenaModel())->getArenasWithTimeTemplateByPaginate();
        return $this->renderSuccess(compact('list'));
    }

    // 新增场地
    public function add()
    {
        $params = Request::only([
            'name',
            'time_template_id',
            'minimum',
            'maximum',
            'status',
            'sort',
        ]);
        $validate = new ArenaValidate();
        if(!$validate->check($params)) {
            return $this->renderError($validate->getError());
        }

        (new ArenaLogic)->addArena($params);
        return $this->renderSuccess('添加成功');
    }

    /**
     * 编辑场地
     * @param int $arena_id
     */
    public function edit()
    {
        $params = Request::only([
            'arena_id',
            'name',
            'time_template_id',
            'minimum',
            'maximum',
            'status',
            'sort',
        ]);

        $validate = new ArenaValidate();
        if(!$validate->check($params)) {
            return $this->renderError($validate->getError());
        }
        
        try{
            (new ArenaLogic())->editArena($params);
        }catch(\Exception $e) {
            return $this->renderError($e->getMessage());
        }

        return $this->renderSuccess([], '编辑场地成功');
    }

    /**
     * 删除场地
     * @param int $arena_id
     */
    public function del()
    {
        $arena_id = Request::param('arena_id');
        try{
            (new ArenaLogic())->deleteArena($arena_id);
        }catch(\Exception $e) {
            return $this->renderError($e->getMessage());
        }

        return $this->renderSuccess([], '删除场地成功');
    }

}
